package JavaSE.CODE.javaNet;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo {
    public static void main(String[] args) {
        try {

            //静态“构造方法”
            //InetAddress类没有构造方法，只能通过静态方法返回对象
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            //  /127.0.0.1
            InetAddress inetAddress1 = InetAddress.getByName("localhost");
            //  localhost/127.0.0.1
            InetAddress inetAddress2 = InetAddress.getLocalHost();
            //  DESKTOP-3KMOT75/172.19.113.191

            InetAddress inetAddress3 = InetAddress.getByName("www.baidu.com");
            //  www.baidu.com/180.101.49.11


            //对象方法
            System.out.println(inetAddress3.getAddress());
            //[B@b1bc7ed
            System.out.println(inetAddress3.getCanonicalHostName());
            //获取规范地址（IP地址）：36.152.44.96
            System.out.println(inetAddress3.getHostAddress());
            //获取IP：36.152.44.96
            System.out.println(inetAddress3.getHostName());
            //获取域名（or自己电脑的名字）：www.baidu.com

        }
        catch (UnknownHostException e){
            e.printStackTrace();
        }
    }
}
